package com.huabo.contract.controller;


import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PropertyFileReader;
import com.huabo.contract.oracle.entity.TblContractInformation;
import com.huabo.contract.oracle.entity.TblContractLend;
import com.huabo.contract.oracle.entity.TblContractPlannode;
import com.huabo.contract.oracle.entity.TblCyhwProjectbudget;
import com.huabo.contract.oracle.entity.TblCyhwUnit;
import com.huabo.contract.oracle.entity.TblMyTask;
import com.huabo.contract.service.TblAttachmentService;
import com.huabo.contract.service.TblCirculationService;
import com.huabo.contract.service.TblContractInformationService;
import com.huabo.contract.service.TblContractLendService;
import com.huabo.contract.service.TblContractPlannodeService;
import com.huabo.contract.service.TblContractTypeofService;
import com.huabo.contract.service.TblCyhwProjectbudgetService;
import com.huabo.contract.service.TblCyhwUnitService;
import com.huabo.contract.service.TblFlowService;
import com.huabo.contract.service.TblMyTaskService;
import com.huabo.contract.service.TblOrganizaService;
import com.huabo.contract.service.TblStaffService;
import com.huabo.contract.util.HttpClient;
import com.huabo.contract.util.Tree;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@Api(value="合同管理Controller",tags={"合同管理所有接口"})
public class ContractManageController {

	private static final String GROUP_STRUCTURE = PropertyFileReader.getItem("group.structure");
	
	@Resource
	private TblCyhwUnitService tblCyhwUnitService;
	@Resource
	private TblContractTypeofService contractTypeofService;

	@Resource
	private TblFlowService tblFlowService;
 
	@Resource
	private TblOrganizaService tblOrganizaService;

	@Resource
	private TblStaffService tblStaffService;

	@Resource
	public TblContractInformationService tblContractInformationService;

	@Resource
	private TblContractPlannodeService tblContractPlannodeService;

	@Resource
	public TblAttachmentService attachmentService;

	@Resource
	private TblContractLendService tblContractLendService;

	@Resource
	private TblCyhwProjectbudgetService tblCyhwProjectbudgetService;

	@Resource
	private TblAttachmentService tblAttachmentService;
	@Resource
	private TblMyTaskService tblMyTaskService;

	
	
	/**
	 * 核验借阅状态--需在借阅期内并且审核状态通过
	 * @param request
	 * @param contractId
	 * @return
	 */
	@ApiOperation(value="获取合同借阅状态")
	@RequestMapping(value = "/contract/checkLendStatus",method = {RequestMethod.GET})
	public JsonBean checkLendStatusA(HttpServletRequest request,@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
			@ApiParam(name="contractId",value="借阅合同Id",required=true)@RequestParam(value="contractId",required=true)String contractId){
		
		JsonBean jsonBean = null;
		try {
			jsonBean =  this.tblCyhwUnitService.checkLendStatus(token, contractId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	
	
	/**
	 * 合同范本列表
	 * @param request
	 * @param pageNumber
	 * @param cyhwUnit
	 * @return
	 */
	@RequestMapping(value = "/cwgl/zcgl_mainck",method = {RequestMethod.POST} ,produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本列表")
	public String zcgl_mainck(HttpServletRequest request,
		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
		@RequestParam(name="flowId",required=true)@ApiParam(name="flowId",value="流程Id主键",required=true)String flowId,
		@RequestParam(name="staffId",required=false)@ApiParam(name="staffId",value="用户Id主键",required=false)String staffId,
		@RequestParam(name="pageNumber",required=false,defaultValue = "1")@ApiParam(name="pageNumber",value="当前页",required=false,defaultValue = "1")Integer pageNumber,
		@RequestParam(name="pageSize",required=false,defaultValue = "10")@ApiParam(name="pageSize",value="每页数量",required=false,defaultValue = "10")Integer pageSize,
		@RequestParam(name="cateId",required=false)@ApiParam(name="cateId",required=false)String cateId,
		@RequestParam(name="isFlowdb",required=false)@ApiParam(name="isFlowdb",required=false)String isFlowdb,
		@RequestParam(name="number",required=false)@ApiParam(name="number",required=false)String number,
		@RequestParam(name="lawyerpersion",required=false)@ApiParam(name="lawyerpersion",required=false)String lawyerpersion,
		@RequestParam(name="xdfaddress",required=false)@ApiParam(name="xdfaddress",required=false)String xdfaddress,
		@ApiParam(name="contractdept",value="承办部门查询主键",required=false)@RequestParam(value="contractdept",required=false)BigDecimal contractdept,
									TblCyhwUnit cyhwUnit){
		String result = null;
		try {
			cyhwUnit.setContractdept(contractdept);
			Map<String,Object>  resultMap = this.tblCyhwUnitService.findListContractBiangengBypageInfo(token,flowId,staffId,pageNumber,pageSize,cyhwUnit);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 合同范本-新建合同类型
	 * @param request
	 * @param flowId 流程Id主键
	 * @param flowname
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/cyhw/choosetContractType",method = {RequestMethod.POST} ,produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-新建合同类型")
	public String choosetContractType(HttpServletRequest request,
											@ApiParam(value="flowid",required=true) BigDecimal flowId,
											@ApiParam(value="flowname",required=false) BigDecimal flowname,
											@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
											@ApiParam(name="staffId",value="用户Id主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.contractTypeofService.findAllListToCreateContract(token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同范本-新建页面
	 *
	 * @param request
	 * @param flowId
	 * @param typeId
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/cyhw/cyhwUnitAdd",method = {RequestMethod.POST} ,produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-新建页面")
	public String cyhwUnitAdd(HttpServletRequest request,@ApiParam(value="flowid",required=true)BigDecimal flowId,
									@ApiParam(value="typeId",required=true)Integer typeId,
									@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
									@ApiParam(name="staffId",value="用户Id主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.contractTypeofService.toAddContract(flowId,typeId,token);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本、合同订立、合同变更-新建修改-保存
	 * @return
	 */

	@ResponseBody
	@RequestMapping(value = "/cyhwUnitSave",method = {RequestMethod.POST} ,produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-新建修改-保存")
	public String cyhwUnitSave(HttpServletRequest request,
							   @ApiParam(name="flowId",value="流程主键Id",required=false)@RequestParam(value="flowId",required=false)String flowId,
							   @ApiParam(name="startdate",required=false)String startdate,
							   @ApiParam(name="enddate",required=false)String enddate,
							   @ApiParam(name="changedate",required=false)String changedate,
							   @ApiParam(name="recordConcat",value="相对方信息主键",required=false)String recordConcat,
							   @ApiParam(name="attids",value="附件Id数组",required=false)String attids,
							   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
							   TblCyhwUnit tcu){
		String result = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			tcu.setCreatetime(new Date());
			if(startdate != null && !"".equals(startdate)) {
				tcu.setStartdate(sdf.parse(startdate));
			}
			if(enddate != null && !"".equals(enddate)) {
				tcu.setEnddate(sdf.parse(enddate));
			}
			if(changedate != null && !"".equals(changedate)) {
				tcu.setChangedate(sdf.parse(changedate));
			}
			Map<String,Object> resultMap = this.tblCyhwUnitService.saveCyhwUnit(tcu,attids,null,token,flowId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			result = "-1";
			e.printStackTrace();
		}
		return result;
	}

	/**
	 *合同范本-修改查询状态
	 * @param request
	 * @param contractId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getCyhwUnitStatue",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-修改查询状态")
	public String getCyhwUnitStatue(HttpServletRequest request,
									@ApiParam(value="contractId",required=true)Integer contractId){
		try {
			Integer statue = tblCyhwUnitService.getStatueById(contractId);
			if(statue!=null && statue==1){
				return JsonBean.error("流程审批中");
			}else if(statue!=null && statue==3){
				return JsonBean.error("流程已通过");
			}else if((statue!=null && statue==4) ||(statue!=null && statue==5) || (statue!=null && statue==6)){
				return JsonBean.error("流程已完成");
			}else if(statue != null && statue == 7) {
				return JsonBean.error("合同正在执行，无法进行此项操作");
			}else if(statue != null && statue == 8) {
				return JsonBean.error("已归档");
			}else if(statue != null && statue == 9) {
				return JsonBean.error("已暂停");
			}else if(statue != null && statue == 10) {
				return JsonBean.error("已变更，请前往合同变更进行此项操作");
			}else if(statue != null && statue == 11) {
				return JsonBean.error("已终止");
			}else if(statue != null && statue == 12) {
				return JsonBean.error("纠纷中");
			}else if(statue != null && statue == 13) {
				return JsonBean.error("协商中");
			}else if(statue != null && statue == 14) {
				return JsonBean.error("诉讼中");
			}else if(statue != null && statue == 15) {
				return JsonBean.error("仲裁中");
			}else{
				return JsonBean.success();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return JsonBean.error("失败");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getCyhwUnitStatueBG",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-修改查询状态")
	public String getCyhwUnitStatueBG(HttpServletRequest request,
									@ApiParam(value="contractId",required=true)Integer contractId){
		try {
			Integer statue = tblCyhwUnitService.getStatueById(contractId);
			if(statue!=null && statue==1){
				//return JsonBean.error("流程审批中");
				return JsonBean.errors();
			}else if(statue!=null && statue==3){
				return JsonBean.errors();
			}else if((statue!=null && statue==4) ||(statue!=null && statue==5) || (statue!=null && statue==6)){
				return JsonBean.errors();
			}else if(statue != null && statue == 7) {
				return JsonBean.errors();
			}else if(statue != null && statue == 8) {
				return JsonBean.errors();
			}else if(statue != null && statue == 9) {
				return JsonBean.errors();
			}else if(statue != null && statue == 10) {
				return JsonBean.errors();
			}else if(statue != null && statue == 11) {
				return JsonBean.errors();
			}else if(statue != null && statue == 12) {
				return JsonBean.errors();
			}else if(statue != null && statue == 13) {
				return JsonBean.errors();
			}else if(statue != null && statue == 14) {
				return JsonBean.errors();
			}else if(statue != null && statue == 15) {
				return JsonBean.errors();
			}else{
				return JsonBean.success();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return JsonBean.error("失败");
		}
	}

	/**
	 * 合同范本-删除
	 * @param request
	 * @param contractId 合同主键
	 * @param flowId
	 * @return
	 */
	@RequestMapping(value = "/cyhwUnitDelete",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本/合同订立-删除")
	public String cyhwUnitDelete(HttpServletRequest request,
									   @ApiParam(value="contractId",required=true)Integer contractId,
									   @ApiParam(value="flowId",required=false)BigDecimal flowId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.removeCyhwUnit(contractId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-提交审批
	 * @param
	 * @return   /process/htgl/tjsp_zcgl
	 */
	@RequestMapping(value = "/cyhwjc/tjsp_zcgl",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-提交审批")
	public @ResponseBody String tjsp_tzglcyhwjc(HttpServletRequest request,
												@ApiParam(name="contractId",value="合同主键",required=false)BigDecimal contractId,
												@ApiParam(name="examination",value="审批意见",required=true)String examination,
												@ApiParam(name="flowid",value="流程主键Id",required=true)String flowId,
												@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
												@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.SubmitForApproval(contractId,token,staffId,examination,flowId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-合同类型列表-右
	 * @param request
	 * @param pageNumber
	 * @param pageSize
	 * @param choiceTypeName 合同类型名称
	 * @param typeId 合同类型主键
	 * @param token
	 * @param staffId
	 * @return
	 */

	@RequestMapping(value = "/contract/typeOfContractList",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同范本-合同类型列表-右")
	public String typeOfContractList(HttpServletRequest request,
										   @ApiParam(name="pageNumber",value="当前页",required=false,defaultValue = "1")Integer pageNumber,
										   @ApiParam(name="pageSize",value="每页数量",required=false,defaultValue = "15")Integer pageSize,
									 		@ApiParam(name="choiceTypeName",value="合同类型名称",required=false)String choiceTypeName,
										   @ApiParam(name="typeId",value="合同类型主键",required=false)String typeId,
										   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
										   @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.contractTypeofService.findPageInfoList(pageNumber,pageSize,typeId,choiceTypeName,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-合同类型-左侧列表
	 * @param choiceSearch
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contract/contractTypeFather", produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation(value="合同范本-合同类型-左侧列表")
	public @ResponseBody String contract_contractTypeFather(@RequestParam(value="choiceSearch",required=false)String choiceSearch,
															@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
															@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		String json = "";
		try {
			List<Tree> list;
			list = this.contractTypeofService.getFatherTree(staff.getCurrentOrg().getOrgid());
			for (Tree tree : list) {
				tree.setTarget("mainFramex");
				tree.setUrl("/cyhw/contract/contractList?pid=&choiceSearch=" +choiceSearch);
				for (Tree ct : tree.getChildren()) {
					ct.setTarget("mainFramex");
					ct.setUrl("/cyhw/contract/contractList?pid=" + ct.getId() +"&choiceSearch=" +choiceSearch);
				}
			}
			json = JSONObject.toJSONString(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return json;
	}

	/**
	 * 合同范本-合同类型-新建保存
	 * @param request
	 * @param
	 * @param parentid  父级id
	 * @param token
	 * @param staffId
	 * @return
	 */

	@RequestMapping(value = "/contract/saveContractType", produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation(value="合同范本-合同类型-新建保存")
	public @ResponseBody String contract_saveContractType(HttpServletRequest request,
														  @ApiParam(name="typeNameArr",required=true) String[] typeName,
														  @ApiParam(name="fatherId",required=true) Integer parentid,
														  @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
														  @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.contractTypeofService.saveContractType(typeName,parentid,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-合同类型-修改
	 * @param request
	 * @param typeId  合同类型主键
	 * @param typeName 合同类型名称
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contract/modifyContractType", produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation(value="合同范本-合同类型-修改")
	public @ResponseBody String contract_modifyContractType(HttpServletRequest request,
															@ApiParam(value="typeId",required=true) String typeId,
															@ApiParam(value="typeName",required=true) String typeName,
															@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
															@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {

			Map<String,Object>  resultMap = this.contractTypeofService.modifyContractType(token,staffId,typeId,typeName);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-合同类型-删除
	 * @param request
	 * @param typeId 合同类型主键
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contract/removeContractType", produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation(value="合同范本-合同类型-删除")
	public @ResponseBody String contract_removeContractType(HttpServletRequest request,
															@RequestParam(value="typeId",required=true) String typeId,
															@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
															@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.contractTypeofService.removeContractType(typeId,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本-办理信息查看
	 * @param request
	 * @param contractId  合同主键Id
	 * @param cyId
	 * @param flowId  流程主键Id
	 * @param token
	 * @param staffId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/htlsjc/to_sptzgl_info", produces = "application/json; charset=utf-8")
	@ApiOperation(value="合同范本、合同订立、合同变更-办理信息查看")
	public  String to_sptzgl_infojc(HttpServletRequest request,
			  @ApiParam(name="contractId",value="合同主键Id",required=true)@RequestParam(value="contractId",required=true)String contractId,
			  @ApiParam(name="flowId",value="流程主键Id",required=true)@RequestParam(value="flowId",required=false)String flowId,
			  @ApiParam(name="token",value="登录用户token  前端必须传这个参数",required=false) @RequestHeader("token") String token,
			  @ApiParam(name="staffId",value="登录用户主键",required=false)@RequestParam(value="staffId",required=false)String staffId) throws Exception{
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.toSptzglInfojc(contractId,null,flowId,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同范本、合同订立、合同变更-查看流程图
	 * @param request
	 * @param contractId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/cyhwjc/approval_process", produces = "application/json; charset=utf-8")
	@ApiOperation(value="合同范本、合同订立、合同变更-查看流程图")
	public String viewOppsiteActiviti(HttpServletRequest request,
									  @ApiParam(name="contractId",value="主键id",required=true)@RequestParam(value="contractId",required=true)String contractId,
									  @ApiParam(name="taskId",value="taskId",required=false)@RequestParam(value="taskId",required=true)String taskId
									  ) throws Exception {
		//taskId  实际上是businesskey   -_-
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.viewOppsiteActiviti(contractId,taskId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/lccj/to_select_processjc",method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
	@ApiOperation(value="合同范本-办理流程审批资料")
	public  String to_select_processjc(HttpServletRequest request,
											 @ApiParam(name="taskid",value="taskid",required=false)String taskId,
											 @ApiParam(name="contractId",value="合同主键Id",required=true)String contractId,
											 @ApiParam(name="flowId",value="流程主键Id",required=true)String flowId,
											 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCirculationService.findeByTaskId(taskId,contractId,flowId,token);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Resource
	private TblCirculationService tblCirculationService;

	/**
	 * 合同订立-新建相对方信息选择按钮
	 * @param request
	 * @param pageNumber
	 * @param pageSize
	 * @param recordType
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/findHtInfo/windowOpen",method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
	@ApiOperation(value="合同订立-新建相对方信息选择按钮")
	public String findHtInfo(HttpServletRequest request,
								   @ApiParam(name="pageNumber",value="当前页",required=false,defaultValue = "1")Integer pageNumber,
								   @ApiParam(name="pageSize",value="每页数量",required=false,defaultValue = "15")Integer pageSize,
								   @ApiParam(name="recordType",value="recordType",required=true)String recordType,
							 @ApiParam(name="budgetname",value="budgetname",required=false)String budgetname,
							 @ApiParam(name="counterpartno",value="counterpartno",required=false)String counterpartno,
								   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
								   @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId) {
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.findListWindowOpenByPageInfo(pageNumber,pageSize,recordType,token,staffId,budgetname,counterpartno);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立-新建执行单位选择按钮
	 * @param response
	 * @param token
	 * @param staffId
	 */
	@RequestMapping(value = "/organ/csfa_findOrganizationByTree",method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
	@ApiOperation(value="合同订立-新建执行单位选择按钮")
	public void csfa_findOrganizationByTree(HttpServletResponse response,
											@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
											@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId) {
		boolean is = false;
		if(StringUtils.isNotBlank(GROUP_STRUCTURE)){
			is = false;
		}
		String orgtree = tblOrganizaService.findOrgByAllGSJT(token,staffId);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.write("[" + orgtree + "]");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

	/**
	  * 合同订立-新建执行部门选择按钮
	  * @param request
	  * @param response
	  * @param str
	  * @param token
	  * @param staffId
	  */
	 @RequestMapping(value = "/findOrganizationByTreeNbkz", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	 @ApiOperation(value = "合同订立-新建执行部门选择按钮")
	 public JsonBean findOrganizationByTreeNukz(HttpServletRequest request, HttpServletResponse response,
	   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token) throws Exception {
		  JsonBean jsonBean = null;
		  try {
		   jsonBean =  this.tblOrganizaService.findOrgDeptTree(token);
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		  return jsonBean;
	 }

	/**
	 * 合同订立-新建执行人选择左侧按钮
	 * @param nodeId
	 * @param type
	 * @param orgId
	 * @param idname
	 * @param textname
	 * @param token
	 * @param staffId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/htdl/findOrganizationByTreeAllss", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-新建执行人选择左侧按钮")
	public @ResponseBody String htdlfindOrganizationByTree(@ApiParam(name="nodeId",value="nodeId",required=false)BigDecimal nodeId,
														   @ApiParam(name="type",value="type",required=false)String type,
														   @ApiParam(name="orgId",value="orgId",required=false)BigDecimal orgId,
														   @ApiParam(name="idname",value="idname",required=false)String idname,
														   @ApiParam(name="textname",value="textname",required=false)String textname,
														   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
														   @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId,
														   HttpServletRequest request) throws Exception {
		String json = "";
		if (null == nodeId) {
			nodeId = orgId;
			if (null == orgId) {
				TblStaffUtil user = DealUserToken.parseUserToken(token);
				nodeId = user.getCurrentOrg().getOrgid();
			}
		}
		if (StringUtils.isNotBlank(type)) {
			List<Tree> list = this.tblOrganizaService.getTrees(nodeId);
			for (Tree tree : list) {
				if (!tree.getIsParent()) {
					tree.setTarget("mainFramex");
					tree.setUrl("/nbkz/pjlx/list?pid=" + tree.getId() + "&idname=" + idname + "&textname=" + textname);
				}
			}
			json = JSONObject.toJSONString(list);
		} else {
			List<Tree> list = this.tblOrganizaService.getNodeAlls(nodeId);
			for (Tree tree : list) {
				setUrlByTree(tree, "/nbkz/pjlx/list?idname=" + idname + "&textname=" + textname + "&pid=");
			}
			json = JSONObject.toJSONString(list);
		}
		return json;
	}

	private void setUrlByTree(Tree tree, String url) {
		for (Tree tre : tree.getChildren()) {
			/// if (!tre.getIsParent()) {
			tre.setTarget("mainFramex");
			tre.setUrl(url + tre.getId());
			// }
			if (tre.getChildren().size() > 0) {
				setUrlByTree(tre, url);
			}
		}
	}

	/**
	 *合同订立-新建执行人选择右侧按钮
	 * @param request
	 * @param pid
	 * @param pageNumber
	 * @param pageSize
	 * @param token
	 * @param staffId
	 * @return
	 */

	@RequestMapping(value = "/htdl/list", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-新建执行人选择右侧按钮")
	public String htdluserListss(HttpServletRequest request,
									   @ApiParam(name="pid",value="pid",required=true)String pid,
									   @ApiParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
									   @ApiParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
									   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
									   @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId) {
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblStaffService.findAllPageBeanPid(pid,pageNumber,pageSize,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同订立-新建立项信息选择按钮
	 *
	 * @param request
	 * @param pageNumber
	 * @param pageSize
	 * @param formId
	 * @param jsonArry
	 * @param paramArry
	 * @param eleName
	 * @param valueId
	 * @param queryType
	 * @param oldFormId
	 * @param textname
	 * @return
	 */
	@RequestMapping(value = "/form/chooseFormValueElePage", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-新建立项信息选择按钮")
	public String form_chooseFormValueElePage(HttpServletRequest request,
													@ApiParam(value="pageNumber",required = false,defaultValue = "1") Integer pageNumber,
													@ApiParam(value="pageSize",required = false,defaultValue = "15") Integer pageSize,
													@ApiParam(value="formId",required = false) Integer formId,
													@ApiParam(value="jsonArry",required = false) String jsonArry,
													@ApiParam(value="paramArry",required = false) String paramArry,
													@ApiParam(value="eleName",required = false) String eleName,
													@ApiParam(value="valueId",required = false) String valueId,
													@ApiParam(value="queryType",required = false) Integer queryType,
													@ApiParam(value="oldFormId",required = false) Integer oldFormId,
											  		@ApiParam(value="eleName",required = false) String textname){


		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblStaffService.findHttpClient(formId,jsonArry,paramArry,eleName,valueId,pageNumber,pageSize,queryType,oldFormId,textname);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同订立-运输货物信息-保存
	 *
	 * @param request
	 * @param information
	 * @return
	 */
	@RequestMapping(value = "/contract/information_toSave", method = {RequestMethod.POST})
	@ApiOperation(value = "合同订立-运输货物信息-保存")
	@ResponseBody
	public String information_toSave(HttpServletRequest request,TblContractInformation information,
									 @ApiParam(value="contractid",required=true)Integer contractid){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractInformationService.saveContractInfoMation(contractid,information);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立-运输货物信息-删除
	 * @param request
	 * @param infoId 货物主键
	 * @return
	 */
	@RequestMapping(value = "/contract/information_toRemove", method = {RequestMethod.POST})
	@ApiOperation(value = "合同订立-运输货物信息-删除")
	@ResponseBody
	public String information_toRemove(HttpServletRequest request,
									   @ApiParam(value="infoId",required=true)Integer infoId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractInformationService.removeContractInfoMation(infoId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同订立-合同阶段信息-新增/修改
	 *
	 * @param request
	 * @param node
	 * @param contractid
	 * @param jbunitid
	 * @param jbstaffid
	 * @return
	 */
	@RequestMapping(value = "/contract/plan_toSave", method = {RequestMethod.POST})
	@ApiOperation(value = "合同订立-合同阶段信息-新增/修改")
	@ResponseBody
	public String plan_toSave(HttpServletRequest request,TblContractPlannode node,
							  @ApiParam(value="contractid",required=true)Integer contractid,
							  @ApiParam(value="jbunitid",required=true)Integer jbunitid,
							  @ApiParam(value="jbstaffid",required=true)Integer jbstaffid){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractPlannodeService.saveContractPlannode(node,contractid,jbunitid,jbstaffid);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立-合同阶段信息-回显
	 * @param request
	 * @param nodeId
	 * @return
	 */
	@RequestMapping(value = "/contract/plan_toModify", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-合同阶段信息-回显")
	public String plan_toModify(HttpServletRequest request,@ApiParam(value="nodeId",required=true)Integer nodeId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractPlannodeService.selectById(nodeId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立-合同阶段信息-删除
	 * @param request

	 * @return
	 */
	@RequestMapping(value = "/contract/plan_toRemove", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-合同阶段信息-删除")
	@ResponseBody
	public String plan_toRemove(HttpServletRequest request,@ApiParam(value="nodeId",required=true)Integer nodeId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractPlannodeService.removeContractPlannode(nodeId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立-附件删除
	 * @param request
	 * @param attid 附件主键
	 * @return
	 */
	@RequestMapping(value = "/contract/deleteFileRelation", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-附件删除")
	public @ResponseBody String deleteFileRelation(HttpServletRequest request,
												   @ApiParam(value="attid",required=true)Integer attid){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.attachmentService.removeAttachmentByattId(attid);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/contract/deleteAttachmentByBedgetId", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同用印-附件删除")
	public @ResponseBody String deleteAttachmentByBedgetId(HttpServletRequest request,
												   @ApiParam(value="attid",required=true)Integer attid){

		String result = null;
		try {
			Map<String,Object> resultMap = this.attachmentService.removeAttachmentByattId(attid);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	/**合同订立-定时提醒
	 *
	 * @param request
	 * @param tcu
	 * @param content
	 * @param date
	 * @param token
	 * @param staffId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/contract/addTipContract", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
	@ApiOperation(value = "合同订立-定时提醒")
	@ResponseBody
	public String addTipContract(HttpServletRequest request,TblCyhwUnit tcu,
								 @ApiParam(value="tipcontent",required=true)String content,
								 @ApiParam(value="tipdate",required=true)String date,
								 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
								 @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId) throws Exception{


		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractPlannodeService.sendTipEmail(tcu,content,date,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同订立/合同变更-提交审批
	 *
	 * @param request
	 * @param contractId
	 * @param examination
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/htgl/tjsp_zcgl",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同订立/合同变更-提交审批")
	public @ResponseBody String htgl_tzglcyhwjc(HttpServletRequest request,
												@ApiParam(name="contractId",value="合同主键",required=false)BigDecimal contractId,
												@ApiParam(name="examination",value="审批意见",required=true)String examination,
												@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
												@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.htglTjsp(contractId,examination,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同订立/合同变更-详情
	 * @param request
	 * @param flowId
	 * @param flowname
	 * @param contractId
	 * @param cflag
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contractToDetail",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同订立/合同变更-详情")
	public String contractToDetail(HttpServletRequest request,
										 @ApiParam(value="flowId",required=true)BigDecimal flowId,
										 @ApiParam(value="flowname",required=false)String flowname,
										 @ApiParam(value="contractId",required=true)Integer contractId,
										 @ApiParam(value="cflag",required=false)Integer cflag,
										 @ApiParam(name="token",value="登录用户token  前端必须传",required=false) @RequestHeader("token") String token,
										 @ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.findeContractInfo(flowId,flowname,contractId,cflag,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同-货物详情
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/contractToInfomationDetail",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同-货物详情")
	public String contractToInfomationDetail(@ApiParam(value="contractId",required=true)Integer contractId){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractInformationService.findInformationListById(contractId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同-阶段详情
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/contractToPlanNodeDetail",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同-阶段详情")
	public String contractToPlanNodeDetail(@ApiParam(value="contractId",required=true)Integer contractId){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractPlannodeService.findPlannodeListById(contractId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同-附件详情
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/contractToAttachmentDetail",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同-附件详情")
	public String contractToAttachmentDetail(@ApiParam(value="contractId",required=true)Integer contractId ){

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblAttachmentService.findAttachmentListById(contractId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}



	/**
	 * 合同订立-引用合同范本
	 * @param request
	 * @param contractType
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "/contract/findContractTemplate",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同订立-引用合同范本")
	@ResponseBody
	public String contract_findContractTemplate(HttpServletRequest request,
												@ApiParam(value="contractType",required=true)String contractType,
												@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token){
		String result = "";
		try {
			List<TblCyhwUnit> unilList = this.tblCyhwUnitService.findContractTemp(token,contractType);
			result = JSON.toJSONString(unilList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同用印-列表
	 * @param request
	 * @param unit
	 * @param pageNumber
	 * @param pageSize
	 * @param cateId
	 * @param flowId
	 * @param isFlowdb
	 * @param choose
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/constract/contractSeal",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同用印-列表")
	public  String constract_contractSeal(HttpServletRequest request, TblCyhwUnit unit,
										 	 @ApiParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
										  	@ApiParam(value = "pageSize",required = false,defaultValue = "15")Integer pageSize,
												@ApiParam(name="cateId",value="cateId",required=false)String cateId,
												@ApiParam(name="flowId",value="flowId",required=true)String flowId,
												@ApiParam(name="isFlowdb",value="isFlowdb",required=false)String isFlowdb,
												@ApiParam(name="condition",value="condition",required=false)String choose,
												@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
												@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.findContractSealList(pageNumber,pageSize,cateId,flowId,isFlowdb,choose,token,staffId,unit);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	@RequestMapping(value = "/projectrBudgetToModify",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同用印-用印回显")
	public String projectrBudgetToModify(HttpServletRequest request,
											   @ApiParam(value="flowid",required=true)BigDecimal flowId,
											   @ApiParam(value="budgetId",required=true)Integer budgetId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwProjectbudgetService.projectrBudgetToModify(flowId,budgetId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同用印-用印新增页面
	 * @param request
	 * @param flowId
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/projectrBudgetToAdd",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同用印-用印新增页面")
	public String projectrBudgetAdd(HttpServletRequest request,
										  @ApiParam(value="flowid",required=true)BigDecimal flowId,
										  @ApiParam(value="contractId",required=true)Integer contractId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwProjectbudgetService.projectrBudgettoAdd(flowId,contractId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 合同用印-用印保存
	 * @param request
	 * @param flowId
	 * @param attids
	 * @param token
	 * @param tcpb
	 * @return
	 */
	@RequestMapping(value = "/projectrBudgetModify",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同用印-用印保存")
	@ResponseBody
	public String projectrBudgetModify(HttpServletRequest request,
									   @ApiParam(name="flowid",value="流程信息主键",required=true)String flowId,
									   @ApiParam(name="createtime",value="用印日期",required=true)String createtime,
									   @ApiParam(name="recordparent",value="合同主键ID",required=false)String contractid,
									   @ApiParam(name="attids",value="附件ID数组",required=false)String attids,
									   @ApiParam(name="sealorgid",value="印章所属主体id",required=true)Integer sealorgid,
									   @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
									   TblCyhwProjectbudget tcpb){
		String result = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(createtime != null && !"".equals(createtime)) {
				tcpb.setCreatetime(sdf.parse(createtime));
			}else{
				tcpb.setCreatetime(new Date());
			}
			if(contractid != null && !"".equals(contractid)){
				tcpb.setRecordparent(Integer.parseInt(contractid));
			}
			Map<String,Object> resultMap = this.tblCyhwProjectbudgetService.insertOrUpdateBybudget(tcpb,token,attids,flowId,sealorgid);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "-1";
		}
		return result;
	}

	//合同用印-办理
//	@RequestMapping(value = "/yszc/to_sptzgl_info")
//	public  String to_sptzgl_infoyszc(HttpServletRequest request,
//											@ApiParam(name="budgetId",value="相对方信息主键",required=true)String budgetId,
//											@ApiParam(name="flowId",value="流程信息主键",required=true)String flowId,
//											@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token
//											) throws Exception{
//		String result = null;
//		try {
//			Map<String,Object>  resultMap = this.tblCyhwProjectbudgetService.findxdfblInfo(budgetId,flowId,token);
//			JSONObject jsonObj = new JSONObject(resultMap);
//			result = jsonObj.toString();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return result;
//	}




	/**
	 * 合同归档列表
	 * @param request
	 * @param unit
	 * @param pageNumber
	 * @param pageSize
	 * @param flowId
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/constract/filContractList",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同归档列表")
	public String filContractList(HttpServletRequest request,TblCyhwUnit unit,
										@ApiParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
										@ApiParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
										@ApiParam(name="flowId",value="flowId",required=true)String flowId,
										@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
										@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.findeFilContractList(pageNumber,pageSize,flowId,token,staffId,unit);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同变更-撤回
	 * @param request
	 * @param contractId
	 * @param flowname
	 * @return
	 */
	@RequestMapping(value = "/cyhw/updBasicUnitinspectionStatue",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同变更-撤回")
	@ResponseBody
	public String updBasicUnitinspectionStatue(HttpServletRequest request,
											   @RequestParam(value="contractId",required=true)Integer contractId,
											   @RequestParam(value="flowname",required=true)String flowname){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.updBasicUnitinspectionStatue(contractId,flowname);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 合同归档-归档
	 * @param request
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/contract/contract_filStatus",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同归档-归档")
	@ResponseBody
	public String contract_filStatus(HttpServletRequest request,
									 @RequestParam(value="contractId",required=true)String contractId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCyhwUnitService.updateContractStatus(contractId,"8");
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**合同归档-借阅保存
	 *
	 * @param request
	 * @param lend
	 * @param contractId
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contract/saveLeadInfo",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同归档-借阅保存")
	@ResponseBody
	public String contract_saveLeadInfo(HttpServletRequest request,TblContractLend lend,
										@ApiParam(value="contractId",required=false)String contractId,
										@ApiParam(value="lenddate",required=true)String lenddate,
										@ApiParam(value="returndate",required=true)String returndate,
										@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
										@ApiParam(name="staffId",value="登录用户Id",required=false)String staffId) {
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractLendService.saveTblContractLead(contractId,lenddate,returndate,token,staffId,lend);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 合同归档-详情
	 * @param request
	 * @param pageNumber
	 * @param pageSize
	 * @param contractId
	 * @param token
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/contract/lendContractList",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同归档-详情")
	public String lendContractList(HttpServletRequest request,
										 @ApiParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
										 @ApiParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
										 @ApiParam(value="contractId",required=true)String contractId,
										 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,
										 @ApiParam(name="staffId",value="登录用户Id",required=false)String staffId){
		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblContractLendService.findByContractId(pageNumber,pageSize,contractId,token,staffId);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/yszc/to_sphtjy_info",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同归档-详情-办理")
	public String to_sphtjy_info( @ApiParam(value="lendid",required=true)String lendid,
										@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token) throws Exception{

		String result = null;
		try {
			Map<String,Object>  resultMap = this.tblCirculationService.findByLendid(lendid,token);
			JSONObject jsonObj = new JSONObject(resultMap);
			result = jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}



	@RequestMapping(value = "/yszc/approval_processhtjy",method = {RequestMethod.POST},produces = "application/html; charset=utf-8")
	@ApiOperation(value="合同借阅--查看流程图片")
	public String approval_processhtjy(@ApiParam(value="lendid",required=true)String lendid,
									   @ApiParam(value="taskId",required=true)String taskId) throws Exception {
		String result = null;
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		Map<String, Object> dataMap = new HashMap<String, Object>(0);
		TblContractLend lend = tblContractLendService.findById(lendid);
		List<TblMyTask> list = tblMyTaskService.findByLendid(lendid);
		dataMap.put("aw", lend);
		dataMap.put("taskList", list);
		dataMap.put("url", HttpClient.jkurl+taskId);
		resultMap.put("code", "1");
		resultMap.put("msg", "成功！");
		resultMap.put("data", dataMap);
		JSONObject jsonObj = new JSONObject(resultMap);
		result = jsonObj.toString();
		return result;
	}

}
